package com.sysu.minsysu.weibo.activity;

import weibo4andriod.Weibo;
import weibo4andriod.WeiboException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sysu.minsysu.R;
import com.sysu.minsysu.weibo.utils.ConfigUtil;


public class HomeActivity extends Activity implements LocationListener{
	public static int MODE = MODE_PRIVATE;
	
	private Weibo weibo;
	private com.szy.weibo.service.Weibo weibo2;
	private EditText et;

	private static String TAG = "HomeActivity"; 
	private String accessToken;
	private String oauth_token_secret;
	private String userId;
	
	private String qq_accessToken;
	private String qq_oauth_token_secret;
	private String qq_userId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weibo_home);
		        
        
        //获取sina微博token和token_secret
        SharedPreferences sharedPreferences = getSharedPreferences("xinlang", MODE);
		accessToken = sharedPreferences.getString("oauth_token", null);
		oauth_token_secret = sharedPreferences.getString("oauth_token_secret", null);
		userId = sharedPreferences.getString("userId", "");		
		Log.d("userId", userId);
		Log.d(TAG, "accessToken: "+ accessToken);
		Log.d(TAG, "oauth_token_secret:" +oauth_token_secret);
		
		//获取qq微博token和token_secret
        sharedPreferences = getSharedPreferences("tengxun", MODE);
        qq_accessToken = sharedPreferences.getString("oauth_token", null);
        qq_oauth_token_secret = sharedPreferences.getString("oauth_token_secret", null);
        qq_userId = sharedPreferences.getString("userId", "");	
		Log.d("qq_userId", userId);
		Log.d(TAG, "qq_accessToken: "+ qq_accessToken);
		Log.d(TAG, "qq_oauth_token_secret:" +qq_oauth_token_secret);
		
		
		System.setProperty("weibo4j.oauth.consumerKey", ConfigUtil.getInstance().getSina_AppKey());
        System.setProperty("weibo4j.oauth.consumerSecret", ConfigUtil.getInstance().getSina_AppSecret());
        weibo = new Weibo();
        weibo.setToken(accessToken, oauth_token_secret);  
        
        weibo2 = new com.szy.weibo.service.Weibo();
		
        
        init();		
	}
	
	private void init(){
		//edittext
		et = (EditText) findViewById(R.id.EditWeibo);		
		//submit button
		Button submitBtn = (Button) findViewById(R.id.submit);
		submitBtn.setOnClickListener(new Button.OnClickListener(){
    		public void onClick(View v){
    			//是否联网
    			ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    			NetworkInfo info = cwjManager.getActiveNetworkInfo(); 
    			
    			if (info != null && info.isAvailable()){ 
	    			//发送sina微博   
	    			if (accessToken != null && oauth_token_secret != null) {
		    			try{
		    				String msg = et.getText().toString();		    				
		    				weibo.updateStatus(msg);
		    				Toast.makeText(HomeActivity.this, "微博发送成功", Toast.LENGTH_SHORT).show();
		    			}catch (WeiboException e){
		    				Toast.makeText(HomeActivity.this, "微博发送失败", Toast.LENGTH_SHORT).show();
							e.printStackTrace();
		    			}	
	    			}
	    			//send qq twitter
	    			String result = "" ;
	    			String msg = et.getText().toString();
	    			result = weibo2.add(qq_accessToken, qq_oauth_token_secret, "json", msg, "127.0.0.1");
	    			Log.i(TAG, result);
    			} else {    				
    				Log.d(TAG, "无法连接网络!");
    			}
    		}
			
    	});

	}


	
	private LocationManager mgr;
    private String best;
    private String latitudeAndLongitude;
    private String address;
	private void updateStat() {	
    	mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    	if (mgr.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
    		Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
    		Log.d(TAG, "GPS successful");      
    	} else {       
	    	Toast.makeText(this, "请开启GPS！", Toast.LENGTH_SHORT).show();
	    	Log.d(TAG, "GPS not successful");
	    	Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
	    	//此为设置完成后返回到获取界面
	    	startActivityForResult(intent,0); 
    	}
    	Criteria criteria = new Criteria();
    	criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度        
    	criteria.setAltitudeRequired(false);        
    	criteria.setBearingRequired(false);        
    	criteria.setCostAllowed(true);        
    	criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
    	best = mgr.getBestProvider(criteria, true); // 获取GPS信息       
    	Location location = mgr.getLastKnownLocation(best); // 通过GPS获取位置   	
//    	Location location = mgr.getLastKnownLocation("gps");
	
    	if (location != null) {  		
    		//pop up
    		StringBuffer msg = new StringBuffer();
    		//获取经纬度
    		msg.append(Double.toString(location.getLatitude()));    		
    		msg.append(",");
    		msg.append(Double.toString(location.getLongitude()));
    		
    		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    		latitudeAndLongitude = msg.toString();
    	} else {
    		Toast.makeText(this, "NO LOCATION FOUND", Toast.LENGTH_SHORT).show();
    		address = "";
    	}
    }	
	
	
	  //toast show when location change
    public void onLocationChanged(Location location) {
    	Toast.makeText(this, location.toString(), Toast.LENGTH_LONG).show();
    }
    
    public void onProviderDisabled(String provider) {
    	
    }
    
    public void onStatusChanged(String provider, int status, Bundle extras) {
    	
    }

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

}
